<?php

namespace Model;

class Map extends \Lib\Model {
	public function getVersion() {
		$iVersion = $this->o->Config->get ( 'info.map_version' );
		if (! is_file ( ROOT . '/DB/139jy_' . $iVersion . '.db' )) {
			$this->_mysql2sqlite ( $iVersion );
		}
		return array (
				'version' => $iVersion,
				'url' => $this->o->Config->get ( 'base.site_url' ) . '/DB/139jy_' . $iVersion . '.db' 
		);
	}
	private function _mysql2sqlite($iVersion) {
		$aTableInfo = $this->o->DB->getTableInfo ( 'map' );
		$aList = $this->o->DB->ls ( array (
				'table' => 'map' 
		) );
		$this->o->DB->Sqlite->connect ( array (
				'name' => ROOT . '/DB/139jy_' . $iVersion . '.db' 
		) );
		$sSql = 'CREATE TABLE "map" ("id" INTEGER PRIMARY KEY NOT NULL %s)';
		foreach ( $aTableInfo ['field'] as $sFieldName => $aInfo ) {
			if ($sFieldName != 'id') {
				$sFieldSql .= ', "' . $sFieldName . '" ' . $aInfo ['Type'];
			}
		}
		$sSql = sprintf ( $sSql, $sFieldSql );
		$this->o->DB->Sqlite->exec ( $sSql );
		foreach ( $aList ['items'] as $aRow ) {
			$this->o->DB->Sqlite->insert ( 'map', $aRow );
		}
		$this->o->DB->Sqlite->disconnect ();
	}
}